import { _decorator, Component, Node, Vec3 } from 'cc'
const { ccclass, property } = _decorator

@ccclass('followPlayer')
export class followPlayer extends Component {
  @property(Node)
  public target: Node

  private offset: Vec3 = null

  start() {
    const playerPos = this.node.position
    const targetPos = this.target.position

    this.offset = new Vec3(playerPos.x - targetPos.x, playerPos.y - targetPos.y, playerPos.z - targetPos.z)
  }

  protected lateUpdate(dt: number): void {
    this.node.setPosition(
      this.target.position.x + this.offset.x,
      this.target.position.y + this.offset.y,
      this.target.position.z + this.offset.z,
    )
  }
}
